// 全局js
import Vue from 'vue';
import axios from 'axios';
import { domain, domain2 } from 'global/setting';
import storage from 'good-storage';

export function reqGet ({ data = {}, url }) {
  return axios({
    method: 'get',
    url,
    data
  });
}

export function reqPost ({ data = {}, url }) {
  return axios({
    method: 'post',
    url: `${domain}${url}`,
    data,
    headers: {
      token: storage.session.get('token')
    }
  })
    .then(({ data }) => {
      let { code, message } = data;
      if (code === '0004') {
        Vue.prototype.$Notice.open({
          desc: `${message},请重新登录`
        });
        window.location.href =
          process.env.NODE_ENV === 'development'
            ? 'http://localhost:3000/login'
            : `${domain2}/hzSystem/login`;
      } else if (code === '0005') {
        Vue.prototype.$Notice.open({
          desc: message
        });
        return { ...data, status: false };
      } else {
        return { ...data, status: true };
      }
    })
    .catch(err => {
      return { err, status: false, message: '请求错误' };
    });
}

export function ajaxPost (obj) {
  let that = Vue.prototype;
  that.$Loading.start();

  return axios({
    method: 'post',
    url: domain + obj.url,
    data: obj.data,
    headers: {
      token: obj.token
    }
  }).then(function (res) {
    switch (res.data.code) {
      case '0004':
        that.$Notice.open({
          desc: `${res.data.message},请重新登录`
        });
        const isDev = process.env.NODE_ENV === 'development';
        if (isDev) {
          window.location.href = `http://localhost:3000/login`;
        } else {
          window.location.href = `${domain2}/hzSystem/login`;
        }
        break;
      case '0005':
        that.$Notice.open({
          desc: res.data.message
        });
        break;
      default:
        obj.success(res);
        break;
    }
  });
}
// 获取文件流的post方法
export function ajaxFilePost (obj) {
  let that = Vue.prototype;
  axios({
    method: 'post',
    responseType: 'blob',
    url: domain + obj.url,
    data: obj.data,
    headers: {
      token: obj.token
    }
  }).then(function (res) {
    obj.success(res);
  });
}
export function ajaxGet (obj) {
  axios({
    method: 'get',
    responseType: 'blob',
    url: domain + obj.url
  }).then(obj.success);
}
Object.assign(Date.prototype, {
  format (format) {
    let o = {
      'M+': this.getMonth() + 1, // month
      'd+': this.getDate(), // day
      'h+': this.getHours(), // hour
      'm+': this.getMinutes(), // minute
      's+': this.getSeconds(), // second
      'q+': Math.floor((this.getMonth() + 3) / 3), // quarter
      S: this.getMilliseconds() // millisecond
    };

    if (/(y+)/.test(format)) {
      format = format.replace(
        RegExp.$1,
        `${this.getFullYear()}`.substr(4 - RegExp.$1.length)
      );
    }

    for (let k in o) {
      if (new RegExp(`(${k})`).test(format)) {
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
        );
      }
    }
    return format;
  }
});
